#include <sstream>
//#include <boost/regex.hpp>
#include <string>
#include <iostream>
#include <memory>
#include <thread>
#include <vector>
#include <algorithm>

const std::string mail  = "^mail from:<[A-Za-z0-9]+@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z0-9]+)>$";

const std::string body_end  = "(.)*\r\n\r\n.\r$";

#define LOG_ERROR(file, lineno, msg) { \
    \
    std::stringstream ss; \
    ss<<"> "<<file <<":"<<lineno<< ": "<< msg;  \
    std::cout<<ss.str() << std::endl;  \
}

//const std::string mail  = "^[A-Za-z0-9]+";
int threadTest();

#if 0
int main (int argc, char** argv) {


  std::string data = "mail from:<vijay@bmc.com>";

  std::string body = "DATE: SAT, 28 DEC 2013 15:55:02 +0530\r\nTO: MAYANK1@REMEX10.LOCAL\r\nFROM: VIJAYMAPI@REMEX10.LOCAL\r\nSUBJECT: TOO1\r\nX-MAILER: SWAKS V20130209.0 JETMORE.ORG/JOHN/CODE/SWAKS/\r\n\r\nTHIS IS A TEST MAILING\r\n\r\n.\r";
  //body += "FROM: VIJAYMAPI@REMEX10.LOCAL\r\nSUBJECT: TOO1\r\nX-MAILER: SWAKS V20130209.0 ";
  //body += "JETMORE.ORG/JOHN/CODE/SWAKS/\r\n\r\nTHIS IS A TEST MAILING\r\n\r\n.\r";

  std::cout<< body.length() <<std::endl;


  try {
    boost::regex self_regex(body_end);


    if (boost::regex_match(body, self_regex)) {
      std::cout<<"Match found" << std::endl;
    }
    else {
      std::cout<<"match failed" << std::endl;
    }
  }
  catch(const std::exception& e) {
    std::stringstream ss;
    ss<<"> "<<__FILE__ <<":"<<__LINE__<< ": "<< e.what();
    std::cout<<ss.str() << std::endl;
  }

  LOG_ERROR(__FILE__, __LINE__, "Hi test");



  threadTest();
}
#endif

typedef std::unique_ptr<std::thread> ThPtr;

void foo() {
  //std::cout << "Hi Test " ;
}

int main() {
  std::vector<ThPtr> tcoll;

  int count =0 ;
  try {
    for(int i=0;i<1000;i++){

      ThPtr th(new std::thread(foo));count++;
      //th->join();
      tcoll.push_back(std::move(th));
    }
    for_each(tcoll.begin(), tcoll.end(), [](ThPtr& th){ th->join(); });
  }
  catch(std::exception& ex) {
    std::cout << ex.what() <<": Thread count "  << count << std::endl;;
  }

  return 0;
}
